package com.wcgksoft.controller.insect;


import com.wcgksoft.common.util.FileUtil;
import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.MultipartFileToFile;
import com.wcgksoft.common.util.comm.StringUtils;
import com.wcgksoft.entity.Plant.PlantEncyclopedias;
import com.wcgksoft.entity.insect.InsectBadPhoto;
import com.wcgksoft.entity.insect.InsectDetection;
import com.wcgksoft.entity.insect.InsectGoodPhoto;
import com.wcgksoft.entity.insect.InsectHistory;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.EnterpriseProjectService;
import com.wcgksoft.service.insect.InsectBadPhotoService;
import com.wcgksoft.service.insect.InsectDetectionService;
import com.wcgksoft.service.insect.InsectGoodPhotoService;
import com.wcgksoft.service.insect.InsectHistoryService;
import com.wcgksoft.service.plant.PlantEncyclopediasService;
import com.wcgksoft.service.plant.PlantService;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 *
 * 虫情监测
 */


@Controller
@RequestMapping("insect/inseinsectDetection")
public class InsectDetectionController {

    @Autowired
    private InsectDetectionService insectDetectionService;

    @Autowired
    private InsectHistoryService insectHistoryService;

    @Autowired
    private InsectBadPhotoService insectBadPhotoService;

    @Autowired
    private InsectGoodPhotoService insectGoodPhotoService;

    @Autowired
    private EnterpriseProjectService enterpriseProjectService;

    @Autowired
    private PlantService plantService;

    @Autowired
    private PlantEncyclopediasService plantEncyclopediasService;




    //查询病虫监测信息
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(HttpServletRequest request,
                         @RequestParam(value = "keywords",required = false)String keywords,
                         @RequestParam(value = "page", required = false) Integer page,
                         @RequestParam(value = "limit", required = false) Integer limit) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        DetachedCriteria criteria = DetachedCriteria.forClass(InsectDetection.class);
        criteria.add(Restrictions.eq("areaId",enterpriseProject.getId()));
        if(StringUtils.isNotEmpty(keywords)) {
            criteria.add(Restrictions.like("type", keywords, MatchMode.ANYWHERE));
        }
        List<InsectDetection> list = insectDetectionService.findByConditions(criteria,page,limit);
        return Layui.data(0, "查询成功", insectDetectionService.getTotalCount(), list);
    }



    /**
     * 查找植物
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public Layui list(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "keyWord", required = false) String keyWord) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        DetachedCriteria criteria = DetachedCriteria.forClass(PlantEncyclopedias.class);
        criteria.add(Restrictions.eq("departid", sysUser.getProId()+""));
        criteria.add(Restrictions.eq("areaid", enterpriseProject.getId()+""));
        if(org.apache.commons.lang.StringUtils.isNotEmpty(keyWord)) {
            criteria.add(Restrictions.like("name", keyWord, MatchMode.ANYWHERE));
        }
        List<PlantEncyclopedias> list = plantEncyclopediasService.findByConditions(criteria, page, limit);
        return Layui.data(0, "查询成功", plantEncyclopediasService.getTotalCount(), list);
    }


    /**
     *  添加病虫监测信息
     */

    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(HttpServletRequest request,
                        @RequestParam(value = "data", required = false) String data,
                        @RequestParam(value = "file",required = false) MultipartFile file)throws Exception{
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("area");
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            InsectDetection insectDetection = (InsectDetection) JSONObject.toBean(JSONObject.fromObject(data), InsectDetection.class);
            if (null != file) {
                FileUtil fileUtil = new FileUtil();
                String uploadFile = fileUtil.uploadFile(MultipartFileToFile.multipartFileToFile(file), "insectDetection");
                insectDetection.setPhoto(uploadFile);
            }
            insectDetection.setAreaId(enterpriseProject.getId());
            insectDetection.setDepartId(sysUser.getProId());
            insectDetection.setTime(new Date());
            insectDetectionService.saveOrUpdate(insectDetection);
            return Layui.data(0, "添加成功", 1, Layui.addList(insectDetection));
        }

    }


    /**
     * 删除
     */

    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id") Integer id) {
        InsectDetection byId = insectDetectionService.findById(id);
        DetachedCriteria criteria = DetachedCriteria.forClass(InsectHistory.class);
        criteria.add(Restrictions.eq("place",byId.getLocationpoints()));
        List<InsectHistory> list = insectHistoryService.findByConditions(criteria);
        for (InsectHistory insectHistory : list) {
            InsectHistory hid = insectHistoryService.findById(insectHistory.getId());
            insectHistoryService.delete(hid);

            DetachedCriteria criteria1 = DetachedCriteria.forClass(InsectBadPhoto.class);
            criteria1.add(Restrictions.eq("historyId",insectHistory.getId()));
            List<InsectBadPhoto> list1 = insectBadPhotoService.findByConditions(criteria1);
            for (InsectBadPhoto insectBadPhoto : list1) {
                InsectBadPhoto bedId = insectBadPhotoService.findById(insectBadPhoto.getId());
                insectBadPhotoService.delete(bedId);
            }

            DetachedCriteria criteria2 = DetachedCriteria.forClass(InsectGoodPhoto.class);
            criteria2.add(Restrictions.eq("historyId",insectHistory.getId()));
            List<InsectGoodPhoto> list2 = insectGoodPhotoService.findByConditions(criteria2);
            for (InsectGoodPhoto insectGoodPhoto : list2) {
                InsectGoodPhoto goodId = insectGoodPhotoService.findById(insectGoodPhoto.getId());
                insectGoodPhotoService.delete(goodId);
            }


        }

        insectDetectionService.delete(byId);

        return Layui.data(1, "删除成功", 1, null);
    }


}
